package cn.gzjp.modules.rice.api.web;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.gzjp.common.config.Global;
import cn.gzjp.common.utils.*;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.modules.gas.service.WxMyPayService;
import cn.gzjp.modules.it.api.bean.CommonJson;
import cn.gzjp.modules.logis.entity.*;
import cn.gzjp.modules.rice.entity.*;
import cn.gzjp.modules.rice.service.*;
import cn.gzjp.modules.wx.entity.WxMpAccount;
import cn.gzjp.modules.wx.service.WxMpAccountService;
import cn.gzjp.modules.wx.service.WxService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;


/**
 * 微信小程序用户接口
 */
@Controller("ApiRiceController")
@Api(value = "五常大米生态原粮业务接口", description = "五常大米生态原粮业务接口")
@RequestMapping("${frontPath}/rice")
public class ApiRiceController extends BaseController implements Serializable {

    @Autowired
    private WxService wxService;
    @Autowired
    private WxMpAccountService wxMpAccountService;
    @Autowired
    private RiceMemberService riceMemberService;

    @Autowired
    private RiceOrderService riceOrderService;

    @Autowired
    private RiceProductService riceProductService;
    @Autowired
    private ProductStyleService productStyleService;
    @Autowired
    private RiceCashCouponService riceCashCouponService;


    /**
     * 登陆接口
     */
    @ApiOperation(value = "用户登录新(返回的是sessionKey，要获得openid请接着调用info接口)")
    @RequestMapping(value = "login", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "code", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "shareOpenid", value = "分享者openid，没有的话传空串", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appId", value = "appId(用于区分不同小程序)", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String login(String code, String shareOpenid, String appId) {
        CommonJson result = new CommonJson();
        log.info("五常大米小程序用户微信登录 code=" + code + "|shareOpenid=" + shareOpenid + "|appId=" + appId);
        if (StrUtil.isEmptyOrUndefined(code)) {
            result.setMsg("code不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        if (StrUtil.isEmptyOrUndefined(appId)) {
            result.setMsg("appId不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        try {
            WxMaJscode2SessionResult session = wxService.getWxMaService(appId).getUserService().getSessionInfo(code);
            String openid = session.getOpenid();
            String unionId = session.getUnionid();
            log.info("openid=" + openid);
            log.info("unionid=" + session.getUnionid());
            if (StringUtils.isNotBlank(openid)) {
                RiceMember user = riceMemberService.get(openid);
                if (user == null) {
                    user = new RiceMember();
                    user.setId(openid);
                    user.setCreateDate(new Date());
                    user.setBalance(new BigDecimal("0"));
                    user.setLevel("0");
                    user.setFriendCount(0);
                    user.setRiceCoupon(0);
                    user.setCashCoupon(0);
                    user.setGiftCard(0);
                    riceMemberService.save(user);//新增用户
                    log.info("用户不存在，创建");
                    if (!StrUtil.isBlankOrUndefined(shareOpenid)) {
                        user.setInviteCode(shareOpenid);//设置推荐人
                        riceMemberService.save(user);
                        RiceMember sharer =  riceMemberService.get(shareOpenid);
                        sharer.setFriendCount(sharer.getFriendCount()+1);
                        riceMemberService.save(sharer);
                    }
                }
            }
            log.info("openid=" + openid + "|sessionKey=" + session.getSessionKey());
            result.setData(session.getSessionKey());
            log.info("返回微信小程序" + mapper.toJson(result));
            return this.mapper.toJson(result);
        } catch (WxErrorException e) {
            this.logger.error(e.getMessage(), e);
            return e.toString();
        }
    }


    /**
     * <pre>
     * 获取用户信息接口
     * </pre>
     */

    @ApiOperation(value = "用户信息获取(新接口,后台可自动获取昵称和头像)")
    @RequestMapping(value = "userInfo", method = RequestMethod.POST)
    @ApiImplicitParams({@ApiImplicitParam(name = "sessionKey", value = "会话密钥", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "encryptedData", value = "消息密文", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "iv", value = "加密算法的初始向量", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "appId", value = "appId(用于区分不同小程序)", dataType = "String", paramType = "form", required = true)
    })
    @ResponseBody
    public String userInfo(String sessionKey, String encryptedData, String iv, String appId) {
        CommonJson result = new CommonJson();
        log.info("五常大米小程序用户获取用户信息 sessionKey=" + sessionKey + "|encryptedData=" + encryptedData + "|iv=" + iv + "|appId=" + appId);
        // 解密用户信息
        WxMaUserInfo userInfo = this.wxService.getWxMaService(appId).getUserService().getUserInfo(sessionKey, encryptedData, iv);
        if (userInfo == null) {
            result.setMsg("小程序获取用户信息失败");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        if (StrUtil.isEmptyOrUndefined(appId)) {
            result.setMsg("appId不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        RiceMember user = riceMemberService.get(userInfo.getOpenId());
        if (user == null) {
            result.setMsg("请先调用登录接口");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        String unionId = userInfo.getUnionId();
        log.info("unionId=" + unionId);
        if (StringUtils.isNotBlank(userInfo.getNickName())) {
            user.setNickName(StringUtils.filterWxNickName(userInfo.getNickName()));
        }
        user.setHeadImg(userInfo.getAvatarUrl());//用户头像
        riceMemberService.save(user);//新增用户
        result.setData(user);
        //log.info(mapper.toJson(result));
        return this.mapper.toJson(result);
    }

    /**
     * 解密获取用户手机号
     *
     * @param sessionKey
     * @param encryptedData
     * @param iv
     * @param openid
     * @return
     */
    @ApiOperation(value = "解密获取用户手机号")
    @RequestMapping(value = "userPhone", method = RequestMethod.POST)
    @ApiImplicitParams({@ApiImplicitParam(name = "sessionKey", value = "会话密钥", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "encryptedData", value = "消息密文", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "iv", value = "加密算法的初始向量", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "form", required = true)
    })
    @ResponseBody
    public String userPhone(String sessionKey, String encryptedData, String iv, String openid) {
        CommonJson result = new CommonJson();
        log.info("(司机端)解密获取用户手机号 sessionKey=" + sessionKey + "|encryptedData=" + encryptedData + "|iv=" + iv + "|openid=" + openid);
        // 解密手机号信息
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid参数不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        UserPhone userPhone = this.wxService.getUserPhone(sessionKey, encryptedData, iv);
        if (userPhone == null) {
            result.setMsg("小程序解密用户手机号失败");
            result.setCode(1);
            log.info(mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        if (StringUtils.isNotBlank(userPhone.getPhoneNumber())) {
            RiceMember logisUser = riceMemberService.get(openid);
            logisUser.setMobile(userPhone.getPhoneNumber());
            riceMemberService.save(logisUser);
        }
        result.setData(userPhone);
        // log.info(mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    /**
     * 个人信息
     */
    @ApiOperation(value = "个人信息")
    @RequestMapping(value = "myInfo", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String myInfo(String openid) {
        CommonJson result = new CommonJson();
        log.info("五常大米个人信息 openid=" + openid);
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        RiceMember user = riceMemberService.get(openid);
        result.setData(user);
        // log.info("返回微信小程序" + mapper.toJson(result));

        return this.mapper.toJson(result);
    }


    /**
     * 文案列表
     */
    @ApiOperation(value = "文案列表")
    @RequestMapping(value = "extension/list", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String extension(String openid) {
        CommonJson result = new CommonJson();
        log.info("五常大米 文案列表 openid=" + openid);
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        RiceProduct search = new RiceProduct();
        List<RiceProduct> productList = riceProductService.findList(search);
        List<Extension> extensions = Lists.newArrayList();
        for(RiceProduct product:productList){
            Extension extension = new Extension();
            extension.setProductId(product.getId());
            extension.setTitle(product.getTitle());
            extension.setPicUrl(product.getPicUrl());
            extensions.add(extension);
        }
        result.setData(extensions);
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }



    /**
     * 产品列表
     */
    @ApiOperation(value = "产品列表")
    @RequestMapping(value = "product/list", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String products(String openid) {
        CommonJson result = new CommonJson();
        log.info("五常大米 产品列表 openid=" + openid);
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        RiceProduct search = new RiceProduct();
        result.setData(riceProductService.findList(search));
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }

    /**
     * 产品详情
     */
    @ApiOperation(value = "产品详情")
    @RequestMapping(value = "product/get", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "productId", value = "产品id", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String product(String productId) {
        CommonJson result = new CommonJson();
        log.info("五常大米 产品详情 productId=" + productId);
        if (StrUtil.isEmptyOrUndefined(productId)) {
            result.setMsg("productId不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        result.setData(riceProductService.get(productId));
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }

    @ApiOperation(value = "提交订单")
    @RequestMapping(value = "submitOrder", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "productIdArray", value = "商品id数组", dataType = "String[]", paramType = "form", required = true),
            @ApiImplicitParam(name = "styleIdArray", value = "规格id数组", dataType = "String[]", paramType = "form", required = true),
            @ApiImplicitParam(name = "numArray", value = "购买数量数组", dataType = "Integer[]", paramType = "form", required = true),
            @ApiImplicitParam(name = "buyerMemo", value = "买家备注", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "signer", value = "收件人姓名", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "province", value = "收货省", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "city", value = "城市", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "area", value = "区", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "address", value = "详细地址", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "cashCouponId", value = "代金券id", dataType = "String", paramType = "form", required = true)

    })
    @ResponseBody
    public String submitOrder(String openid,String[] productIdArray,String[] styleIdArray,int[] numArray,String buyerMemo,String signer,
                              String province,String city,String area,String address,String phone,String cashCouponId){
        CommonJson result = new CommonJson();
        log.info("提交订单 openid=" + openid + "|productIdArray=" + productIdArray + "|numArray=" + numArray + "|buyerMemo=" + buyerMemo + "|signer=" + signer +
                "|province=" + province +
                "|city=" + city + "area=" + area + "|address" + address + "|phone=" + phone+
                "|styleIdArray="+styleIdArray+
                "|cashCouponId="+cashCouponId

        );
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(-1);
            log.info("返回给小程序数据:" + this.mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        if (ObjectUtil.isNull(productIdArray)||productIdArray.length==0) {
            result.setMsg("productIdArray不能为空");
            result.setCode(-1);
            log.info("返回给小程序数据:" + this.mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        if(productIdArray.length!=numArray.length){
            result.setMsg("productIdArray,numArray参数不匹配");
            result.setCode(-1);
            log.info("返回给小程序数据:" + this.mapper.toJson(result));
            return this.mapper.toJson(result);
        }
        BigDecimal totalPrice = new BigDecimal("0");
        for(int i=0;i<styleIdArray.length;i++){
            String styleId = styleIdArray[i];
            int num = numArray[i];
            String productId = productIdArray[i];
            RiceProduct product = riceProductService.get(productId);
            ProductStyle style = productStyleService.get(styleId);
            product.setSaleCount(product.getSaleCount()+num);
            riceProductService.save(product);
            totalPrice = totalPrice.add(style.getDiscountPrice().multiply(new BigDecimal(num)));
        }
        int discout = 0;
        if(StrUtil.isNotBlank(cashCouponId)){
            RiceCashCoupon coupon = riceCashCouponService.get(cashCouponId);
            discout = coupon.getAmount();
            coupon.setStatus("1");
            riceCashCouponService.save(coupon);

        }
        totalPrice = totalPrice.subtract(new BigDecimal(discout));
        RiceOrder order = new RiceOrder();
        order.setId(FlowIdUtil.getInstance().getFlowidNoYear()+"");
        order.setOpenid(openid);
        order.setBuyState(0);
        order.setDeliveryState(0);
        order.setTotalPrice(totalPrice);
        order.setBuyTime(new Date());
        order.setSigner(signer);
        order.setBuyerMemo(buyerMemo);
        order.setProvince(province);
        order.setCity(city);
        order.setArea(area);
        order.setAddress(address);
        order.setPhone(phone);
        riceOrderService.submit(order,productIdArray,styleIdArray,numArray);
        result.setData(riceOrderService.get(order.getId()));
        log.info(mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    /**
     * 我的订单列表
     */
    @ApiOperation(value = "我的订单列表")
    @RequestMapping(value = "orders", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "0未支付 1已支付2已取消",
                    dataType = "String", paramType = "query")
    })
    @ResponseBody
    public String myOrders(String openid, int status) {
        CommonJson result = new CommonJson();
        log.info("五常大米 我的订单列表 openid=" + openid + "|status=" + status);
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }

        RiceOrder search = new RiceOrder();
        search.setBuyState(status);
        search.setOpenid(openid);

        result.setData(riceOrderService.findList(search));
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }

    /**
     * 订单详情
     */
    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "order/get", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单id", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String order(String orderId) {
        CommonJson result = new CommonJson();
        log.info("五常大米 订单详情 orderId=" + orderId);
        if (StrUtil.isEmptyOrUndefined(orderId)) {
            result.setMsg("orderId不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        result.setData(riceOrderService.get(orderId));
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    @Autowired
    private WxMyPayService wxPayService;

    /**
     * 返回前台H5调用JS支付所需要的参数，公众号支付调用此接口
     *
     * @param
     * @param request
     * @return
     */
    @ApiOperation(value = "微信支付所需参数")
    @RequestMapping(value = "getJSSDKPayInfo", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "form", required = true)
    })
    @ResponseBody
    public String getJSSDKPayInfo(String openid, String orderId, String appId, HttpServletRequest request) {
        log.info("getJSSDKPayInfo 拉起微信支付 orderId=" + orderId + "|openid=" + openid + "|appId=" + appId);
        CommonJson result = new CommonJson();
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        if (StrUtil.isEmptyOrUndefined(appId)) {
            result.setMsg("appId不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        if (StrUtil.isEmptyOrUndefined(orderId)) {
            result.setMsg("orderId不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }

        WxMpAccount wxMpAccount = wxMpAccountService.get(appId);
        String macid = wxMpAccount.getMchId();
        String key = wxMpAccount.getMchKey();
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
        String notifyUrl = basePath + Global.getFrontPath() + "/wxPayback/riceNotify/" + orderId;
        RiceOrder order = this.riceOrderService.get(orderId);
        if (order == null) {
            result.setCode(-1);
            result.setMsg("订单不存在");
            log.info("返回微信小程序" + mapper.toJson(result));
            return this.mapper.toJson(result);
        }

        if (StrUtil.isNotBlank(order.getWxOrderId())) {
            result.setMsg("已支付，无需重复支付");
            log.info("返回微信小程序" + mapper.toJson(result));
            return this.mapper.toJson(result);
        }

        String body = "支付订单" + order.getTotalPrice() + "元";


        BigDecimal totalMoney = order.getTotalPrice();//order.getDeposit();


        String json = this.wxPayService.getPayInfo(orderId, body, totalMoney, appId, macid, openid, request, key, notifyUrl, "");
        result.setData(json);
        return this.mapper.toJson(result);
    }

    public static Logger payLog = LoggerFactory.getLogger("payLog");

    @ApiOperation(value = "微信支付日志回调(最终支付状态以微信支付回调接口为主)")
    @RequestMapping(value = "wxPayOk", method = RequestMethod.POST)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单id", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "openid", value = "支付者openid", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "formId", value = "formId", dataType = "String", paramType = "form", required = true),
    })
    @ResponseBody
    public String wxPayOk(String orderId, String type, String openid, String formId) {
        payLog.info("支付成功调用wxPayOk orderId=" + orderId + "type=" + type + "|openid=" + openid + "|formId=" + formId );
        CommonJson result = new CommonJson();
        RiceOrder order = this.riceOrderService.get(orderId);
        if (order == null) {
            result.setCode(-1);
            result.setMsg("订单不存在");
            payLog.info("返回微信小程序" + mapper.toJson(result));
            return this.mapper.toJson(result);
        }

        order.setBuyState(1);
        riceOrderService.save(order);

        result.setData(order);
        payLog.info("调用wxPayOk 成功！");
        return this.mapper.toJson(result);
    }



    /**
     * 我的好友
     */
    @ApiOperation(value = "我的好友列表")
    @RequestMapping(value = "friend/list", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String friends(String openid) {
        CommonJson result = new CommonJson();
        log.info("五常大米 好友列表 openid=" + openid);
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        RiceMember search = new RiceMember();
        search.setInviteCode(openid);
        result.setData(riceMemberService.findList(search));
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    @Autowired
    private RiceCouponLogService riceCouponLogService;
    /**
     * 我的粮票列表
     */
    @ApiOperation(value = "我的粮票列表")
    @RequestMapping(value = "riceCoupon/list", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String riceCoupons(String openid) {
        CommonJson result = new CommonJson();
        log.info("五常大米 我的粮票列表 openid=" + openid);
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        RiceCouponLog search = new RiceCouponLog();
        search.setGainOpenid(openid);
        result.setData(riceCouponLogService.findList(search));
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }


    /**
     * 我的代金券列表
     */
    @ApiOperation(value = "我的代金券列表")
    @RequestMapping(value = "cashCoupon/list", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "openid", dataType = "String", paramType = "query", required = true)
    })
    @ResponseBody
    public String cashCoupons(String openid) {
        CommonJson result = new CommonJson();
        log.info("五常大米 我的粮票列表 openid=" + openid);
        if (StrUtil.isEmptyOrUndefined(openid)) {
            result.setMsg("openid不能为空");
            result.setCode(1);
            return this.mapper.toJson(result);
        }
        RiceCashCoupon search = new RiceCashCoupon();
        search.setOpenid(openid);
        result.setData(riceCashCouponService.findList(search));
        log.info("返回微信小程序" + mapper.toJson(result));
        return this.mapper.toJson(result);
    }

}